import { Component, OnInit, OnDestroy } from '@angular/core';

import { Util } from '../../utils/util';

import { ProductComponentService } from './product.component.service';


import { ProductBase } from './product.base';
import {ToastyService} from '../../modules/ng2-toasty';
@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css'],
  providers: [ProductComponentService]
})
export class ProductComponent extends ProductBase {

  // cat option
  option: any = {
    defaultText: true,
    all:true
  };


  // 参数
  param: any = {
    q: '',
    start: 0,
    rows: 10,
    sort: 'sellMoney',
    sortType: 'desc',
    shopType:''
  };

  constructor(public util: Util,public toastyService:ToastyService, public service: ProductComponentService) {

    super(service,util,toastyService );

    if (this.util.sessionStore().has('product')) {

      let _store = this.util.sessionStore().get('product');

      Object.assign(this.param, _store.param);

      console.log(_store);

      if (_store.cat && (typeof _store.cat.wholeName != "undefined")) {
        this.option.cat = _store.cat.wholeName == '' ? "/" : _store.cat.wholeName;
      }

      this.getHttp(this.param);

    }

  }



  ngOnDestroy() {
    this.util.sessionStore().set('product', { param: this.param, cat: this.catStore });
    if (this.busy) {
      this.busy.unsubscribe();

    }
  }


  // shopType Change
  shopTypeChange(data) {

    console.log(data);

    // 第一页
    this.pageChanged(1);

  }



  // 宝贝关键词搜索
  qStore: any={
    q:'',
    uid:''
  };
  search() {
    if (this.param.q === this.qStore.q && this.param.uid == this.qStore.uid) {
      return;
    }

    this.qStore.q = this.param.q;
    this.qStore.uid = this.param.uid;
    this.getHttp(this.param);
  }


  getHttp(param) {

     this.vm.table.products.length = 0;

    this.busy = this.service.getProduct(param).subscribe((res: any) => {

      this.vm.table.products = res.data.value;

      this.vm.table.count = res.data.count;

    }, (err: any) => {

    });
  }

}
